<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Category;
use App\Http\Model\Images;
use App\Http\Model\Logs;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;

class ImageController extends Controller
{
    /**
     * 添加图册
     * @return $this|array
     */
    public function addImg(){
        check_auth([3,5]);
        if($input=Input::except('_token')){
            if(count($input['images'])==1){
                return ['status'=>0,'msg'=>'未上传图片'];
            }
            $images=[];
            foreach($input['images'] as $i){
                if($i!='0'){
                    $images[]=$i;
                }
            }
            $input['images']=json_encode($images);
            $input['user_id']=Session::get('admin')['id'];
            if(Images::create($input)){
                Logs::create(['username'=>Session::get('admin')['username'],'event'=>'-添加了相册->'.$input['title']]);
                return ['status'=>1,'msg'=>'保存完成'];
            }else{
                return ['status'=>0,'msg'=>'保存失败'];
            }
        }
        $category=Category::where(['type'=>2,'status'=>1])->get()->all();
        return view('admin.images.addimg',compact('category'))->with(['title'=>'添加图片']);
    }

    /**
     * 图册与回收站
     * @param $status 状态
     * @return $this
     */
    public function imglist($status){
        check_auth([3,5]);
        $user_id=Session::get('admin')['id'];
        $endTime=$startTime=$titler=$category_id='';
        $imglist=Images::select('images.title','images.describe','user.username','images.images','images.created_at','images.id','category.category')
            ->join('category','category.id','=','images.category_id','left')->orderBy('created_at','desc');
        if(!empty(Session::get('admin')['is_author'])){
            $imglist=$imglist->join('user','user.id','=','images.user_id','left')->where(['images.status'=>$status,'images.user_id'=>$user_id]);
        }elseif(empty(Session::get('admin')['is_author'])){
            $imglist=$imglist->join('user','user.id','=','images.user_id','left')->where(['images.status'=>$status]);
        }
        if($search=Input::except('page')){
            $startTime=$search['start_time'];
            $endTime=$search['end_time'];
            $titler=$search['title'];
            $category_id=$search['category_id'];
            if(!empty($category_id)){
                $imglist=$imglist->where('category_id','=',$category_id);
            }
            if(!empty($titler)){
                $imglist=$imglist->where('images.title','like',"%".$titler."%");
            }
            if(!empty($startTime)){
                $imglist=$imglist->where('images.created_at','>',strtotime($startTime));
            }
            if(!empty($endTime)){
                $endTimer=strtotime($endTime)+3600*24;
                $imglist=$imglist->where('images.created_at','<',$endTimer);
            }
            $imglist=$imglist->paginate(3)->appends(['title' => $titler,'start_time'=>$startTime,'end_time'=>$endTime,'category_id'=>$category_id]);
        }else{
            $imglist=$imglist->paginate(3);
        }
        $category=Category::where(['type'=>2])->get()->all();
        return $status?view('admin.images.imglist',compact('imglist','category','titler','category_id','startTime','endTime'))->with(['title'=>'图片列表']):
            view('admin.images.recyclebin',compact('imglist','category','titler','category_id','startTime','endTime'))->with(['title'=>'图片回收站']);
    }

    /**
     * 更新图册状态
     * @return array
     */
    public function updateImageStatus(){
        check_auth([3,5]);
        if($input=Input::all()){
            $status=$input['status']?0:1;
            if(Images::where(['id'=>$input['id']])->update(['status'=>$status])){
                Logs::create(['username'=>Session::get('admin')['username'],'event'=>'-更改了相册'.$input['id'].'的状态']);
                return ['status'=>1,'msg'=>'修改完成'];
            }else{
                return ['status'=>0,'msg'=>'修改失败'];
            }
        }
    }

    /**
     * 删除图册
     * @return array
     */
    public function deleteImage(){
        check_auth([3,5]);
        if($input=Input::all()){
            $id=$input['id'];
            $images=json_decode(Images::select('images')->find($id)['images']);
            if(Images::where(['id'=>$id])->delete()){
                $disk = Storage::disk('qiniu');
                foreach($images as $k=>$v){
                    if(!empty($v)){
                        $imgName=basename($v);
                        if($disk->exists($imgName)){
                            $disk->delete($imgName);
                        }
                    }
                }
                Logs::create(['username'=>Session::get('admin')['username'],'event'=>'-删除了相册->id->'.$id.'']);
                return ['status'=>1,'msg'=>'删除完成'];
            }else{
                return ['status'=>0,'msg'=>'删除失败'];
            }
        }
    }

    /**
     * 编辑图册
     * @param $id 图册id
     * @return $this
     */
    public function editImg($id){
        check_auth([3,5]);
        $imginfo=Images::find($id);
        $category=Category::where(['type'=>2,'status'=>1])->get()->all();
        return view('admin.images.editimg',compact('category','imginfo'))->with(['title'=>'添加图片']);
    }

    /**
     * 删除一个图片
     * @return array
     */
    public function deleteOneImg(){
        check_auth([3,5]);
        if($imgname=Input::except('_token')){
            $disk = Storage::disk('qiniu');
            $imgName=$imgname['imgname'];
            if($disk->exists($imgName)){
                $disk->delete($imgName);
            }
            Logs::create(['username'=>Session::get('admin')['username'],'event'=>'-删除了一张图片'.$imgName]);
            return ['status'=>1,'msg'=>'图片已删除'];
        }
    }

    /**
     * 更新图册
     * @return array
     */
    public function updateImg(){
        check_auth([3,5]);
        if($input=Input::except('_token')){
            $images=[];
            foreach($input['images'] as $i){
                if($i!='0'){
                    $images[]=$i;
                }
            }
            $id=$input['id'];
            unset($input['id']);
            $input['images']=json_encode($images);
            if(Images::where(['id'=>$id])->update($input)){
                Logs::create(['username'=>Session::get('admin')['username'],'event'=>'-更新了相册'.$id.'->'.$input['title']]);
                return ['status'=>1,'msg'=>'更新成功'];
            }else{
                return ['status'=>0,'msg'=>'更新失败'];
            }
        }

    }
}
